//
//  MenkSegment.m
//  分页
//
//  Created by Burn on 2019/4/22.
//  Copyright © 2019 menksoft. All rights reserved.
//

#import "MenkSegment.h"

@interface MenkSegment ()

@property(strong,nonatomic) NSArray *items_;
@property(strong,nonatomic) NSMutableArray *labels;


@end

@implementation MenkSegment

- (instancetype)initWithItems:(NSArray *)items{
    if (self = [super initWithItems:items]) {
        self.items_ = items;
        
        NSDictionary *dic = @{
                              //1.设置字体样式:例如黑体,和字体大小
                              NSFontAttributeName:[UIFont fontWithName:@"Arial-BoldMT" size:20],
                              //2.字体颜色
                              NSForegroundColorAttributeName:[UIColor clearColor]
                              };
        [self setTitleTextAttributes:dic forState:UIControlStateNormal];
    }
    self.clipsToBounds = YES;
    [self addbuttons];
    [self removeSub];
    return self;
}




- (void)addbuttons{
    for (int i = 0; i < self.items_.count; i++) {
        UILabel *label = [[UILabel alloc]init];
        label.transform = CGAffineTransformScale(label.transform, -1, 1);
        label.transform =  CGAffineTransformRotate(label.transform, M_PI / 2);
        label.font = [UIFont fontWithName:@"MenksoftQagan_mirror" size:14];
        label.text = self.items_[i];
        label.textColor = [UIColor redColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.tag = 3000+i;
        [self.labels addObject:label];
        [self addSubview:label];
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    for (int i = 0; i < self.labels.count; i++) {
        UILabel *label = (UILabel *)self.labels[i];
        label.frame = CGRectMake(1 + i * self.bounds.size.width / self.items_.count, 1, self.bounds.size.width / self.items_.count - 1 - i, self.bounds.size.height - 2);
        label.backgroundColor = [UIColor clearColor];
    }
}

- (NSMutableArray *)labels{
    if (!_labels) {
        _labels = [NSMutableArray array];
    }
    return _labels;
}

- (void)removeSub{
    for (UIView * view in self.subviews) {
        if ([view isKindOfClass:NSClassFromString(@"UISegmentLabel")]) {
            [view removeFromSuperview];
        }
    }
}

@end
